<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Resources\Api\Admin\AdminRoleResource;
use App\Http\Resources\Api\Admin\AdminRoleRouteResource;
use App\Models\Admin\AdminRole;
use App\Http\Requests\Api\Admin\AdminRoleRequest;
use App\Http\Requests\Api\Admin\AdminRoleUpdateRequest;
use App\Http\Queries\AdminRoleQuery;
use App\Http\Controllers\Api\Controller;
use Illuminate\Http\Request;

class RoleManageController extends Controller
{
    /**
     * 得到所有的角色
     */
    public function roles()
    {
        $res = AdminRoleResource::collection(AdminRole::all());
        return $this->success($res);
    }

    /**
     * 添加角色
     * @param AdminRoleRequest $request
     * @param AdminRole $role
     * @return mixed
     */
    public function add(AdminRoleRequest $request, AdminRole $role)
    {
        $role->role_name = $request->input('role_name');
        $role->role_content = $request->input('role_content');
        $role->save();

        return $this->success(new AdminRoleResource($role));
    }

    /**
     * 更新角色
     * @param AdminRoleUpdateRequest $request
     * @return mixed
     */
    public function update(AdminRoleUpdateRequest $request)
    {
        $role = AdminRole::find($request->input('role_id'));
        $attributes = $request->only(['role_name', 'role_content']);

        $role->update($attributes);

        return $this->success(new AdminRoleResource($role));
    }


    /**
     * 查询角色
     * @param AdminRoleQuery $query
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function index(AdminRoleQuery $query)
    {
        $roles = $query->paginate($this->getPageSize());
        return AdminRoleResource::collection($roles);
    }

}
